﻿Imports Heren.Common.VectorEditor
Imports Heren.Common.VectorEditor.Shapes
Imports Heren.Common.Report
Imports Heren.Common.Report.Runtime


Public Class 新生儿危重护理记录单 : Inherits AbstractRuntimeHandler
    ''****************************************************************
    '' 这是护理电子病历表单报表后台运行支持脚本
    '' 注意：这是Visual Basic脚本，您可以不区分大小写 
    '' 请不要修改系统提供的方法名，以及方法名前面的Overrides关键字
    '' Script Language : Visual Basic
    '' Author : YangMingkun, Date : 2012-6-6
    '' Copyright (C) Heren Health Services (SUPCON) Co.,Ltd.
    ''****************************************************************

    '报表脚本初始化
    Public Overrides Sub Initialize()

    End Sub

    Private Sub SetElementVisible(ByVal visible As Boolean)
       





        '设置界面上的数框为空
        Dim elements As ElementBase() = Me.FindElement("文本")
        If elements IsNot Nothing Then
            For i As Integer = 0 To elements.Length - 1
                elements(i).Visible = visible
            Next
        End If

        Dim element As ElementBase = Me.FindOneElement("姓名")
        element.Visible = visible

        element = Me.FindOneElement("科别")
        element.Visible = visible

        element = Me.FindOneElement("床号")
        element.Visible = visible


        element = Me.FindOneElement("性别")
        element.Visible = visible


        element = Me.FindOneElement("住院ID号")
        element.Visible = visible

        elements = Me.FindElement("直线段")
        If elements IsNot Nothing Then
            For i As Integer = 0 To elements.Length - 1
                elements(i).Visible = visible
            Next
        End If

        element = Me.FindOneElement("页码")
        element.Visible = visible

        element = Me.FindOneElement("矩形")
        element.Visible = visible

        For index As Integer = 1 To 24
            Dim line As LineElement = Me.FindOneElement("直线段" & index)
            If line IsNot Nothing Then
                line.Visible = visible
            End If
        Next
    End Sub
    '更新病人信息
    Private Sub UpdatePatientInfo()
        Dim value As New Object
        Me.GetSystemContext("病人姓名", value)
        Me.SetElementValue("姓名", value)

        Me.GetSystemContext("用户科室名称", value)
        Me.SetElementValue("科别", value)

        Me.GetSystemContext("床号", value)
        Me.SetElementValue("床号", value)

        Me.GetSystemContext("住院号", value)
        Me.SetElementValue("住院号", value)

        Me.GetSystemContext("病人ID号", value)
        Me.SetElementValue("ID", value)


    End Sub
    Private ContinuedPrintIndex As Integer = 0
    Private ContinuedPrint As Boolean = False
    Private pageIndex As Integer = 1
    '更新表单报表
    Public Overrides Function UpdateReport(ByVal param As String, ByVal data As Object) As Boolean
        If param = "续打行号" Then
            Me.ContinuedPrintIndex = CType(data, Integer)
        ElseIf param = "是否续打" Then
            Me.ContinuedPrint = CType(data, Boolean)
        ElseIf param = "打印数据" Then
            Me.UpdatePatientInfo()
            Me.PrintData(data)
        End If
    End Function


    ''填充实际的打印数据
    Public Sub PrintData(ByVal data As DataTable)

        Dim element As RowColumnElement = CType(Me.FindOneElement("护理记录单"), RowColumnElement)

        Dim RowCount As Integer = data.Rows.Count

        '计算开始行
        If data.Columns.Contains("pageIndex") Then

            pageIndex = Me.ToIntValue(Me.GetFieldValue(data, 0, "pageIndex"), 1)
            'showmessage("打印一 "+pageIndex.tostring)
        Else
            data = Me.GetPrintData(data) '对打印数据进行处理
            '计算打印页码
            For index As Integer = 0 To data.Rows.Count
                Dim printed As Boolean = Me.ToBoolValue(Me.GetFieldValue(data, index, "printed"), True)
                If printed Then

                    pageIndex = Int((index) / 14) * 2 + 1

                    Exit For
                End If
            Next index
            


        End If
        Dim table_row_count As Integer = data.Rows.Count

        Dim BeginRowIndex As Integer = (pageIndex - 1) * 14 / 2  '当前页的索引为 1 则打印从 第0行开始 打印14行
        Dim EndRowIndex As Integer = BeginRowIndex + 13 '结束页的行号
        If table_row_count < EndRowIndex Then
            EndRowIndex = table_row_count '最后一页 行数小于14行
        End If
        Dim row_index As Integer = 0 '报表行号 从0开始 到13
        ' showmessage("table_row_count "+table_row_count.tostring)
        ' showmessage("BeginRowIndex "+BeginRowIndex.tostring)
        ' showmessage("EndRowIndex "+EndRowIndex.tostring)
        For index As Integer = BeginRowIndex To EndRowIndex
            Dim printed As Boolean = Me.ToBoolValue(Me.GetFieldValue(data, index, "printed"), True)
            If index = 0 Then
                If Me.ContinuedPrint AndAlso printed = False Then
                    Me.SetElementVisible(False)
                Else
                    Me.SetElementVisible(True)
                End If
            End If
            If Not printed AndAlso Me.ContinuedPrint Then
                row_index = row_index + 1
                Continue For
            End If

            Dim dateTimeStr As String = Me.GetFieldValue(data, index, "Column1").ToString()
            '日期和时间
            If dateTimeStr.Trim().Length > 5 Then
                Dim time As DateTime = Me.ToTimeValue(dateTimeStr, DateTime.Now)
                Me.SetTableCellValue("护理记录单", row_index, "日/月", time.ToString("MM/dd"))
                Me.SetTableCellValue("护理记录单", row_index, "时间", time.ToString("HH:mm"))
            Else
                Me.SetTableCellValue("护理记录单", row_index, "日/月", "")
                Me.SetTableCellValue("护理记录单", row_index, "时间", dateTimeStr)
            End If

            '箱温/湿度
            Me.SetTableCellValue("护理记录单", row_index, "箱温/湿度", Me.GetFieldValue(data, index, "箱温/湿度"))
            '体温
            Me.SetTableCellValue("护理记录单", row_index, "体温", Me.GetFieldValue(data, index, "体温"))
            '脉搏
            Me.SetTableCellValue("护理记录单", row_index, "脉搏", Me.GetFieldValue(data, index, "脉搏"))
            '呼吸
            Me.SetTableCellValue("护理记录单", row_index, "呼吸", Me.GetFieldValue(data, index, "呼吸"))
            '血压
            Me.SetTableCellValue("护理记录单", row_index, "血压", Me.GetFieldValue(data, index, "血压"))
            '血氧饱和度
            Me.SetTableCellValue("护理记录单", row_index, "血氧饱和度", Me.GetFieldValue(data, index, "血氧饱和度"))
            '血糖
            Me.SetTableCellValue("护理记录单", row_index, "血糖", Me.GetFieldValue(data, index, "血糖"))
            '经皮胆红素
            Me.SetTableCellValue("护理记录单", row_index, "经皮胆红素", Me.GetFieldValue(data, index, "经皮胆红素"))
            '神志
            Me.SetTableCellValue("护理记录单", row_index, "神志", Me.GetFieldValue(data, index, "神志"))
            '瞳孔对光反射
            Me.SetTableCellValue("护理记录单", row_index, "瞳孔对光反射", Me.GetFieldValue(data, index, "瞳孔对光反射"))
            '吸吮
            Me.SetTableCellValue("护理记录单", row_index, "吸吮", Me.GetFieldValue(data, index, "吸吮"))
            '末梢温度
            Me.SetTableCellValue("护理记录单", row_index, "末梢温度", Me.GetFieldValue(data, index, "末梢温度"))
            '药物名称
            Me.SetTableCellValue("护理记录单", row_index, "药物名称", Me.GetFieldValue(data, index, "药物名称"))
            '药物量
            Me.SetTableCellValue("护理记录单", row_index, "药物量", Me.GetFieldValue(data, index, "药物量"))
            '母乳/水
            Me.SetTableCellValue("护理记录单", row_index, "母乳/水", Me.GetFieldValue(data, index, "母乳/水"))
            '配方奶
            Me.SetTableCellValue("护理记录单", row_index, "配方奶", Me.GetFieldValue(data, index, "配方奶"))
            row_index = row_index + 1
        Next index

        Me.SetElementValue("页码", "第 " & pageIndex.ToString() & " 页")
        '通知系统当前页打印完成
        Me.NotifyPageCompleted()
        pageIndex = pageIndex + 1
        '标记打印到第几页
        data.Rows(0)("pageIndex") = pageIndex.ToString()
        '清空表格，以及将表格行索引恢复为0
        element.ClearTableText()
        Me.NotifyNextReport("2新生儿危重护理记录单") '通知打印反面的报表

    End Sub


    ''获取分行后的datatable
    Public Function ReturnSeparate(ByVal strvalue As String, ByVal Width_CuoShi As Integer) As String

        '获取列宽度和字体,以便计算分行
        Dim Font_CuoShi As Font = New Font("宋体", 9.0F)

        Dim arrCuoShi As String() = Me.GetTextLines(strvalue, Font_CuoShi, Width_CuoShi)
        Dim ReturnStr As String = String.Empty
        For i As Integer = 0 To arrCuoShi.Length - 1
            ReturnStr += arrCuoShi(i) + vbCrLf
        Next i
        Return ReturnStr
    End Function


    ''获取打印的所有行数据
    Public Function GetPrintData(ByVal PrintData As DataTable) As DataTable

        '获取列宽度和字体，以便计算分行
        '入量
        Dim Width_RuLingName As Integer = 60
        Dim Font_RuLiangName As Font = New Font("宋体", 7.0F)
        Dim Element As TextFieldElement = Me.FindElement("入量名称_1")(0) '报表界面上放了一个元素 方便得到入量名称需要的行宽
        If Element IsNot Nothing Then
            Width_RuLingName = Element.Width
            Font_RuLiangName = Element.TextFont
        End If
        '出量
        Dim Width_ChuLiangName As Integer = 60
        Dim Font_ChuLiangName As Font = New Font("宋体", 7.0F)
        Element = Me.FindElement("出量名称_1")(0)  '报表界面上放了一个元素 方便得到出量名称需要的行宽
        If Element IsNot Nothing Then
            Width_ChuLiangName = Element.Width
            Font_ChuLiangName = Element.TextFont
        End If
        '病情处置
        Dim Width_BingQingChuZhi As Integer = 60
        Dim Font_BingQingChuZhi As Font = New Font("宋体", 7.0F)
        Element = Me.FindElement("病情处置_1")(0)
        If Element IsNot Nothing Then
            Width_BingQingChuZhi = Element.Width
            Font_BingQingChuZhi = Element.TextFont
        End If
        '深拷贝
        Dim data As DataTable = PrintData.Copy()
        PrintData.Rows.Clear()

        ''打印标识字段
        PrintData.Columns.Add("printed")
        ''打印到第几页
        PrintData.Columns.Add("pageIndex")

        ''处理同一天的记录只显示日期一次,跨年份显示年份
        Dim arrDateStr(data.Rows.Count) As String

        For rowIndex As Integer = 0 To data.Rows.Count - 1
            Dim RecordTime1 As DateTime
            'showmessage(Me.GetFieldValue(data, rowIndex, "Column1"))
            DateTime.TryParse(Me.GetFieldValue(data, rowIndex, "Column1"), RecordTime1)
            For nextRowIndex As Integer = rowIndex + 1 To data.Rows.Count - 1
                Dim RecordTime2 As DateTime

                DateTime.TryParse(Me.GetFieldValue(data, nextRowIndex, "Column1"), RecordTime2)
                If RecordTime1.Date = RecordTime2.Date Then
                    arrDateStr(nextRowIndex) = RecordTime2.ToString("HH:mm")
                    rowIndex = rowIndex + 1
                ElseIf RecordTime1.Date <> RecordTime2.Date AndAlso RecordTime1.Year <> RecordTime2.Year Then
                    arrDateStr(nextRowIndex) = RecordTime2.ToString("yyyy-MM-dd HH:mm:ss")
                    Exit For

                Else
                    Exit For
                End If
            Next nextRowIndex
        Next rowIndex

        For rowIndex As Integer = 0 To data.Rows.Count - 1

            'showmessage(arrDateStr(rowIndex))
            If arrDateStr(rowIndex) Is Nothing Then

                'data.Rows(rowIndex)("Column1") = data.Rows(rowIndex)("Column1").SubString(5, 5) + ";" + data.Rows(rowIndex)("Column1").SubString(11, 5)
            ElseIf arrDateStr(rowIndex).Length <= 5 Then
                data.Rows(rowIndex)("Column1") = arrDateStr(rowIndex)
            Else
                data.Rows(rowIndex)("Column1") = arrDateStr(rowIndex).Substring(0, 10) + ";" + arrDateStr(rowIndex).Substring(11, 5)
            End If
        Next rowIndex


        ''分行处理
        For rowIndex As Integer = 0 To data.Rows.Count - 1
            ''对一些特殊列分行
            Dim CellValue As String = Me.GetFieldValue(data, rowIndex, "出量名称")
            Dim arrChuLiangName As String() = Me.GetTextLinesByEnter(CellValue)

            CellValue = Me.GetFieldValue(data, rowIndex, "出量")
            Dim arrChuLiangValue As String() = Me.GetTextLines(CellValue, Font_ChuLiangName, 1000)

            CellValue = Me.GetFieldValue(data, rowIndex, "药物名称")
            Dim arrRuLiangName As String() = GetTextLinesByEnter(CellValue)

            CellValue = Me.GetFieldValue(data, rowIndex, "药物量")
            Dim arrRuLiangValue As String() = Me.GetTextLines(CellValue, Font_RuLiangName, 1000)

            CellValue = Me.GetFieldValue(data, rowIndex, "病情处置、护理及效果")
            Dim arrBingQingChuZhi As String() = Me.GetTextLines(CellValue, Font_BingQingChuZhi, 1000)

            '计算出最大行数
            Dim Max_Line_Count As Integer = 1

            If Max_Line_Count < arrChuLiangName.Length Then Max_Line_Count = arrChuLiangName.Length
            If Max_Line_Count < arrChuLiangValue.Length Then Max_Line_Count = arrChuLiangValue.Length
            If Max_Line_Count < arrRuLiangName.Length Then Max_Line_Count = arrRuLiangName.Length
            If Max_Line_Count < arrRuLiangValue.Length Then Max_Line_Count = arrRuLiangValue.Length
            If Max_Line_Count < arrBingQingChuZhi.Length Then Max_Line_Count = arrBingQingChuZhi.Length

            ''创建新的DataTable
            For index As Integer = 0 To Max_Line_Count - 1
                '创建行的值的数组
                Dim Values(data.Columns.Count - 1) As String
                '给值的数组赋值
                For columnIndex As Integer = 0 To data.Columns.Count - 1
                    Values(columnIndex) = String.Empty
                    If index = 0 Then
                        Values(columnIndex) = Me.GetFieldValue(data, rowIndex, columnIndex)
                    End If
                    If data.Columns(columnIndex).ColumnName = "药物名称" Then
                        Values(columnIndex) = Me.GetArrayItem(arrRuLiangName, index)
                    ElseIf data.Columns(columnIndex).ColumnName = "药物量" Then
                        Values(columnIndex) = Me.GetArrayItem(arrRuLiangValue, index)
                    ElseIf data.Columns(columnIndex).ColumnName = "出量名称" Then
                        Values(columnIndex) = Me.GetArrayItem(arrChuLiangName, index)
                    ElseIf data.Columns(columnIndex).ColumnName = "出量" Then
                        Values(columnIndex) = Me.GetArrayItem(arrChuLiangValue, index)
                    ElseIf data.Columns(columnIndex).ColumnName = "病情处置、护理及效果" Then
                        Values(columnIndex) = Me.GetArrayItem(arrBingQingChuZhi, index)
                    End If
                Next columnIndex
                PrintData.Rows.Add(Values)
                If rowIndex < Me.ContinuedPrintIndex Then
                    PrintData.Rows(PrintData.Rows.Count - 1)("printed") = False
                Else
                    PrintData.Rows(PrintData.Rows.Count - 1)("printed") = True
                End If
            Next index
        Next rowIndex



        Return PrintData
    End Function

    '按换行符对指定的字符串文本进行分行
    Private Function GetTextLinesByEnter(ByVal text As String) As String()
        If text Is Nothing Then
            Return New String() {""}
        End If
        Dim lines As New List(Of String)
        lines.AddRange(text.Split(vbCrLf))

        '去除分出来的多余空白行
        Dim index As Integer = lines.Count - 1
        While index >= 0
            If lines(index).Trim() = "" Then lines.RemoveAt(index)
            index = index - 1
        End While

        Return lines.ToArray()
    End Function
End Class
